package com.hp.itsm.api.examples;

import com.hp.itsm.api.*;
import com.hp.itsm.api.interfaces.*;
import com.hp.ifc.util.ApiDateUtils;
import java.util.Date;
import java.util.TimeZone;
import java.util.Locale;

/**
 * This example shows how to open an existing servicecall when you know
 * its functional ID. It then shows various properties of the Service call.
 * and of related objects.
 * The example retrieves the history lines belonging to the service call.
 * One of the properties of a history line is the date of the change that
 * it describes.
 * Service Desk stores dates in Doubles. The API offers methods to
 * convert them to Java dates. The example illustrates these methods.
 */

public class Example2
{
    /**
     * arguments :
     * server		: the server to connect to
     * username		: the username of the account that you want to use
     * password		: the password of the account that you want to use
     * id			: the functional id of the service call that you want to open
     */
    public static void main(String args[]) {
        if (args.length != 4) {
            System.err.println("Usage : Example2 <server> <username> <password> <id>");
            return;
        }
        String server = args[0];
        String username = args[1];
        String password = args[2];
        long id = Long.valueOf(args[3]).longValue(); //note : in 4.0 this used to be an int.

        ApiSDSession session = null;

        try {
            session = ApiSDSession.openSession(server, username, password);
        } catch (RuntimeException e) {
            // Something went wrong while logging on, show error
            System.out.println(e.getMessage());
            return;
        }

        //	Get the home of the servicecall entity
        IServicecallHome scHome = session.getServicecallHome();
        IServicecall serviceCall = null;

        //	Open the servicecall with the functional id provided.
        //	The functional ID of a service call is the number that
        //	identifies it to the external world.
        //	This is a database call that can fail for that reason and
        //	because no service call with that functional ID exists.
        //	Make sure to catch the exception.
        try {
            serviceCall = scHome.openServicecall(id);
        } catch (RuntimeException e) {
            //	Datbase error or service call does not exist.
            //	Give message and give up.
            System.out.println(e.getMessage());
            return;
        }

        // Show the description of this servicecall
        System.out.println( scHome.getLabelDescription()+ ": "+ serviceCall.getDescription());

        // Get the status of the service call, and show the text belonging to
        // the status.
        IStatusServicecall status = serviceCall.getStatus();
        if (status != null) {
            System.out.println( scHome.getLabelStatus()+ ": "+ status.getText());
            // Find all servicecalls with the same status.
            // The number of service calls with a particular status can be huge so
            // this might require a lot of resources. Fortunately Service Desk
            // imposes a restriction on the number of objects that a query can
            // return. This saves us on the performance front, but the restriction
            // might have more influence on the number that we print here than the
            // actual number of service calls with this status.
            IServicecall[] otherScs = status.getServicecall_Status();
            System.out.println("There are " + otherScs.length + " service calls with status " + status.getText());
        }

        // Show the impact of this servicecall
        IImpact impact = serviceCall.getImpact();
        if (impact != null) {
            System.out.println( scHome.getLabelImpact()+ ": "+ impact.getText());
        }

        // Get the caller of this servicecall
        IPerson caller = serviceCall.getCaller();

        // Get all servicecalls of which this person is the caller
        if (caller != null) {
            IServicecall[] scs = caller.getServicecalls();
            System.out.println(caller.getName() + " is the caller of " + scs.length + " servicecalls.");
        }

        // Get the timezone for this account.
        String sTimezone = session.getCurrentAccount().getDefaultTimeZone().getSearchcode();
        TimeZone timezone = com.hp.ifc.sys.AppTimeZone.getTimeZone(sTimezone);

        // Get the historylines of this servicecall, and for each historyline
        // get some properties and show them.
        IHistoryLineServicecall[] historyLines = serviceCall.getHistoryLines();

        //	Get the home for history lines. We need it to obtain the labels.
        IHistoryLineServicecallHome hlHome = session.getHistoryLineServicecallHome();

        for (int i = 0; i < historyLines.length; i++) {
            // Get the creation date of this history line.
            // NOTE that Service Desk stores dates as Java Doubles.
            Double created = historyLines[i].getRegistration().getCreated();

            // Convert the Java Double representation of the creation date
            // to a Java Date object.
            // This is an example of the conversion of Service Desk dates to
            // Java Dates.
            Date javaDate = ApiDateUtils.double2Date(created);

            // Represent the double that contains the creation date as a String.
            // Use the timezone of the current account.
            // This illustrates the use of the Service Desk date formatting methods.
            String stringDate = ApiDateUtils.double2LocalizedJavaDate(created, java.util.Locale.ENGLISH, timezone);

            // get the creator of the historyline
            IAccount creator = historyLines[i].getRegistration().getCreatedby();

            // show all info of historyline
            System.out.println( "Historyline " + i + " :");
            System.out.println( "    "+ hlHome.getLabelRegistration()+ ": "+ creator.getDisplayName());
            System.out.println( "    "+ "Created on : " + stringDate + " " + sTimezone );
            System.out.println( "    "+ hlHome.getLabelSubject()+ ": "+ historyLines[i].getSubject());
        }
    }

    public static final String REVISION = "$Revision: 3 $";
}
